//
//  UIView+ATFrame.m
//  IPDirectConnection
//
//  Created by lianglibao on 2017/11/11.
//  Copyright © 2017年 100TAL. All rights reserved.
//

#import "UIView+ATFrame.h"

@implementation UIView (ATFrame)

- (CGFloat)width {

    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {

    CGRect frame = self.frame;

    frame.size.width = width;

    self.frame = frame;
}

- (CGFloat)height {

    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {

    CGRect frame = self.frame;

    frame.size.height = height;

    self.frame = frame;
}

- (CGFloat)minX {

    return CGRectGetMinX(self.frame);
}

- (CGFloat)minY {

    return CGRectGetMinY(self.frame);
}

- (CGFloat)maxX {

    return CGRectGetMaxX(self.frame);
}

- (CGFloat)maxY {

    return CGRectGetMaxY(self.frame);
}

- (CGFloat)x {

    return self.minX;
}

- (void)setX:(CGFloat)x {

    CGRect frame = self.frame;

    frame.origin.x = x;

    self.frame = frame;
}

- (CGFloat)y {

    return self.minY;
}

- (void)setY:(CGFloat)y {

    CGRect frame = self.frame;

    frame.origin.y = y;

    self.frame = frame;
}

- (CGPoint)boundsCenter {
    return CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
}

- (void)setSize:(CGSize)size {
    self.width = size.width;
    self.height = size.height;
}

- (CGSize)size {
    return self.frame.size;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (void)setOrigin:(CGPoint)origin {
    self.x = origin.x;
    self.y = origin.y;
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setTop:(CGFloat)top {
    self.y = top;
}

- (CGFloat)top {
    return self.y;
}

- (void)setBottom:(CGFloat)bottom {
    self.y = bottom - self.height;
}

- (CGFloat)bottom {
    return self.y + self.height;
}

- (void)setLeft:(CGFloat)left {
    self.x = left;
}

- (CGFloat)left {
    return self.x;
}

- (void)setRight:(CGFloat)right {
    self.x = right - self.width;
}

- (CGFloat)right {
    return self.x + self.width;
}

/**
 *  判断一个控件是否与主窗口重叠
 * [self convertRect:self.bounds toView:nil] nil代表主窗口
 */
- (BOOL)at_intersectWithView:(UIView*)view {
    if (view == nil)
        view = [UIApplication sharedApplication].delegate.window;

    CGRect rect1 = [self convertRect:self.bounds toView:nil];

    CGRect rect2 = [view convertRect:view.bounds toView:nil];

    return CGRectIntersectsRect(rect1, rect2);
}


- (void)setBoundsScale:(CGFloat)scale {
    self.bounds = CGRectMake(0, 0, self.width * scale, self.height * scale);
}

- (void)sizeScale:(CGFloat)scale {
    CGSize size = self.size;
    size.width *= scale;
    size.height *= scale;
    self.size = size;
}

/**
 * invoke layoutSubview in this runloop
 */
- (void)at_layoutSubviewsImmediately {
    [self setNeedsLayout];
    [self layoutIfNeeded];
}
- (void)at_setFrameAndTriggerLayoutIfNeeded:(CGRect)frame {
    CGRect bounds = self.bounds;
    [self setFrame:frame];

    // for iOS5, otherwise layoutSubviews may not be triggered sometimes
    if (!CGRectEqualToRect(bounds, self.bounds))
        [self setNeedsLayout];
}

@end
